// pages/MusicIndex/musicCity/musicCity.js
const MusicHttp = require("../../../utils/MusicHttp");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hotCities: [],
        laterCities: [],
        indexList: [],
        cities: [],
        arr: []
    },
    clickMe(e) {
        var { aname } = e.currentTarget.dataset;
        wx.setStorageSync("city", aname);
        wx.navigateBack();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        var cityRes = await MusicHttp.getCityHttp();
        this.handleCityHttp(cityRes);
    },
    handleCityHttp(res) {
        res = res.data.data;
        var keys = Object.keys(res.cities);
        var arr = [];
        keys.forEach(item => {
            arr.push({
                firstLetter: item,
                list: res.cities[item]
            })
        })
        this.setData({
            hotCities: res.hotCities,
            indexList: Object.keys(res.cities),
            cities: res.cities,
            arr
        })
    },
    onSearch(event) {
        var cities = this.data.cities;
        var laterCities = this.data.laterCities;
        var values = Object.values(cities);
        values.forEach(item => {
            item.forEach(e => {
                if (e.name.includes(event.detail)) {
                    if (laterCities.includes(event.detail)) {
                        laterCities.splice(laterCities.indexOf(event.detail), 1);
                        console.log(laterCities.indexOf(event.detail));
                        laterCities.unshift(event.detail);
                    } else {
                        laterCities.unshift(e.name);
                    }
                }
            })
        })
        this.setData({
            laterCities
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})